package ownserver;

import java.io.Serializable;

import base.Ship;

import com.sun.sgs.app.AppContext;
import com.sun.sgs.app.Channel;
import com.sun.sgs.app.ChannelManager;
import com.sun.sgs.app.DataManager;
import com.sun.sgs.app.Delivery;
import com.sun.sgs.app.ManagedObject;
import com.sun.sgs.app.ManagedReference;
import com.sun.sgs.app.util.ScalableList;


public class ServerShip extends Ship implements ManagedObject, Serializable {
	/** The version of the serialized form of this class. */
    private static final long serialVersionUID = 1L;
    
    private ManagedReference<ScalableList<ServerTurret>> turretsRef = null;
    
    private static final String PUBLIC_CHANNEL_BIND_PREFIX = "Ship.public.";
    
    public ServerShip (int guid, int typeid, double x, double y, int ownerID, double hp) {
    	super(guid, typeid, x, y, hp);
    	
    	ScalableList<ServerTurret> turrets = new ScalableList<ServerTurret> ();
    	
    	ChannelManager channelManager = AppContext.getChannelManager();
    	
    	String cname = PUBLIC_CHANNEL_BIND_PREFIX + guid;
    	
    	Channel pubchan = channelManager.createChannel(cname, null, Delivery.RELIABLE);
    	
    	DataManager dataManager = AppContext.getDataManager();
        dataManager.markForUpdate(this);
        
        publicRef = dataManager.createReference(pubchan);
        turretsRef = dataManager.createReference(turrets);
        this.ownerID = ownerID;
    }
    
    public String toString()
    {
    	return Integer.toString(getGuid());
    }
    
    private ManagedReference<Channel> publicRef;
    protected int ownerID;
    
    public Channel getPublicChannel()
    {
    	return publicRef.get();
    }
    
    public int getOwnerID() {
    	return ownerID;
    }
    
    public void attachTurret(ServerTurret turret) {
    	turretsRef.getForUpdate().add(turret);
    }
    
    public ServerTurret getTurret(int index) {
    	return turretsRef.get().get(index);
    }
    
    public int getNumTurrets() {
    	return turretsRef.get().size();
    }
    
    public void setAttackTarget(ManagedReference<ServerShip> tgt)
	{
		AppContext.getDataManager().markForUpdate(this);
		targetRef = tgt;
	}
    
    public ManagedReference<ServerShip> getAttackTargetRef() {
		return targetRef;
	}
    
    private ManagedReference<ServerShip> targetRef = null;
    
    public void setX(double newX)
	{
		AppContext.getDataManager().markForUpdate(this);
		super.setX(newX);
	}
    
    public void setY(double newY)
	{
		AppContext.getDataManager().markForUpdate(this);
		super.setY(newY);
	}
    
    public void setMoveTarget(double x, double y)
	{
		AppContext.getDataManager().markForUpdate(this);
		super.setMoveTarget(x, y);
	}
    
    public void setHeading(double h)
	{
		AppContext.getDataManager().markForUpdate(this);
		super.setHeading(h);
	}
    
    public void setVelocity(double velocity) {
		AppContext.getDataManager().markForUpdate(this);
		super.setVelocity(velocity);
	}
    
    public void setAcceleration(double acceleration) {
		AppContext.getDataManager().markForUpdate(this);
		super.setAcceleration(acceleration);
	}
    
    public void setRotation(double rotation) {
		AppContext.getDataManager().markForUpdate(this);
		super.setRotation(rotation);
	}
    
    public void setPlannedHeading(double plannedHeading) {
		AppContext.getDataManager().markForUpdate(this);
		super.setPlannedHeading(plannedHeading);
	}
    
    public void setPlannedVelocity(double plannedVelocity) {
		AppContext.getDataManager().markForUpdate(this);
		super.setPlannedVelocity(plannedVelocity);
	}
    
    public void setPlannedX(double plannedX) {
		AppContext.getDataManager().markForUpdate(this);
		super.setPlannedX(plannedX);
	}
    
    public void setPlannedY(double plannedY) {
		AppContext.getDataManager().markForUpdate(this);
		super.setPlannedY(plannedY);
	}
}
